package org.zbclan.bf2.rcon.home.client.gpo;

import java.util.HashMap;
import java.util.List;

import org.vaadin.gwtgraphics.client.DrawingArea;
import org.vaadin.gwtgraphics.client.animation.Animate;
import org.vaadin.gwtgraphics.client.shape.Circle;
import org.vaadin.gwtgraphics.client.shape.Path;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.gwt.user.client.ui.Widget;

public class GPOView extends Composite implements GPODisplay {

	private static final int HIGHLIGHT_INTERVAL = 1000;

	private static GPOViewUiBinder uiBinder = GWT.create(GPOViewUiBinder.class);

	interface GPOViewUiBinder extends UiBinder<Widget, GPOView> {
	}

	private HashMap<Object, CheckBox> checkboxes = new HashMap<Object, CheckBox>();
	private HashMap<Object, Anchor> templateAnchors = new HashMap<Object, Anchor>();
	private HashMap<Object, Anchor> controlPointAnchors = new HashMap<Object, Anchor>();
	private HashMap<Object, Circle> objectCircles = new HashMap<Object, Circle>();
	private HashMap<Object, Circle> controlPointCircles = new HashMap<Object, Circle>();

	// private ShapeBuilder shapeBuilder;
	// private DirectShapeRenderer mapRenderer;
	private float scale;
	private float xCenter;
	private float yCenter;

	private Animate objectHighlightAnimate;
	private Timer objectHighlightTimer;
	private Circle objectHighlight;

	private Animate controlPointHighlightAnimate;
	private Timer controlPointHighlightTimer;
	private Circle controlPointHighlight;

	@UiField
	protected HasWidgets objectSpawnerContainer;

	@UiField
	protected HasWidgets controlPointsContainer;

	@UiField
	protected HasWidgets combatAreaManagerContainer;

	@UiField
	protected FlexTable objectFlexTable;

	@UiField
	protected DrawingArea mapDrawingArea;

	@UiField
	protected HasClickHandlers saveClickable;

	/**
	 * 
	 */
	public GPOView() {

		initWidget(uiBinder.createAndBindUi(this));

		int col = 0;
		objectFlexTable.setText(0, col++, "#");
		objectFlexTable.setText(0, col++, "Enabled");
		// objectFlexTable.setText(0, col++, "Template");
		objectFlexTable.setText(0, col++, "Team 1");
		objectFlexTable.setText(0, col++, "Team 2");
		objectFlexTable.setText(0, col++, "Layer");
		// objectFlexTable.setText(0, col++, "Position");
		// objectFlexTable.setText(0, col++, "Rotation");
		objectFlexTable.setText(0, col++, "Control Point");

		mapDrawingArea.setWidth(400);
		mapDrawingArea.setHeight(400);

		return;
	}

	public Widget asWidget() {
		return this;
	}

	public void startProcessing() {
	}

	public void stopProcessing() {
	}

	public void addObject(Object key, String template, String team1,
			String team2, String layer, String position, String rotation,
			String setControlPointId, boolean isEnabled) {

		int rowCount = objectFlexTable.getRowCount();
		int col = 0;

		objectFlexTable.setText(rowCount, col++, rowCount + "");
		objectFlexTable.setWidget(rowCount, col++, createEnabledCheckBox(key,
				isEnabled));
		// objectFlexTable.setWidget(rowCount, col++, createTemplateAnchor(key,
		// template));
		objectFlexTable.setText(rowCount, col++, team1);
		objectFlexTable.setText(rowCount, col++, team2);
		objectFlexTable.setText(rowCount, col++, layer);
		// objectFlexTable.setText(rowCount, col++, position);
		// objectFlexTable.setText(rowCount, col++, rotation);
		objectFlexTable.setWidget(rowCount, col++, createControlPointAnchor(
				key, setControlPointId));

		/*
		 * draw
		 */
		String[] coor = position.split("/");
		float x = Float.parseFloat(coor[0]);
		// float z = Float.parseFloat(coor[1]);
		float y = Float.parseFloat(coor[2]);

		mapDrawingArea.add(createObjectCircle(key, x, y));

		return;
	}

	private Circle createObjectCircle(Object key, float x, float y) {

		Circle circle = new Circle((int) (x * scale + xCenter), (int) (-y
				* scale + yCenter), 1);

		objectCircles.put(key, circle);

		return circle;
	}

	private Circle createControlPointCircle(Object key, float x, float y) {

		Circle circle = new Circle((int) (x * scale + xCenter), (int) (-y
				* scale + yCenter), 1);
		circle.setStrokeColor("red");

		controlPointCircles.put(key, circle);

		return circle;
	}

	private Anchor createControlPointAnchor(Object key, String setControlPointId) {

		Anchor a = new Anchor(setControlPointId);

		controlPointAnchors.put(key, a);

		return a;
	}

	private Anchor createTemplateAnchor(Object key, String template) {
		Anchor a = new Anchor(template);

		templateAnchors.put(key, a);

		return a;
	}

	private Widget createEnabledCheckBox(Object key, boolean isEnabled) {

		CheckBox c = new CheckBox();
		c.setValue(isEnabled);

		checkboxes.put(key, c);

		return c;
	}

	public HasValue<Boolean> getEnabledChangeable(Object key) {
		return checkboxes.get(key);
	}

	public HasClickHandlers getControlPointClickable(Object key) {
		return controlPointAnchors.get(key);
	}

	public HasClickHandlers getTemplateClickable(Object key) {
		return templateAnchors.get(key);
	}

	public HasClickHandlers getSaveClickable() {
		return saveClickable;
	}

	public DrawingArea getMapDrawingArea() {
		return mapDrawingArea;
	}

	@UiFactory
	DrawingArea makeMapDrawingArea() {
		return new DrawingArea(400, 400);
	}

	public void adjustCoordinates(float scale, float xCenter, float yCenter) {
		this.scale = scale;
		this.xCenter = xCenter;
		this.yCenter = yCenter;
	}

	public void addArea(List<String> areaPoints) {

		int size = areaPoints.size();
		if (size > 0) {

			// display.createArea(area.getCreate());
			// DirectShapeRenderer areaRenderer = new DirectShapeRenderer(
			// mapDrawingArea);

			// -49.101013/-466.630981
			String point = areaPoints.get(0);
			String[] coor = point.split("/");
			float x = Float.parseFloat(coor[0]);
			float y = Float.parseFloat(coor[1]);

			// display.beginArea(x, y);
			// areaRenderer.beginPath().moveTo(x * scale + xCenter,
			// -y * scale + yCenter);
			Path areaShape = new Path((int) (x * scale + xCenter), (int) (-y
					* scale + yCenter));

			for (int i = 1; i < size; i++) {
				point = areaPoints.get(i);
				coor = point.split("/");
				x = Float.parseFloat(coor[0]);
				y = Float.parseFloat(coor[1]);

				// display.addAreaPoint(x, y);
				// areaRenderer.drawLineTo(x * scale + xCenter, -y * scale
				// + yCenter);
				areaShape.lineTo((int) (x * scale + xCenter),
						(int) (-y * scale + yCenter));
			}

			// display.closeArea();
			// areaRenderer.closePath().stroke();
			areaShape.close();
			mapDrawingArea.add(areaShape);
		}

		return;
	}

	public HasClickHandlers getMapObjectClickable(Object key) {
		return objectCircles.get(key);
	}

	public void highlightObject(final Object key) {
		// objectShapes.get(key).removeStyleName(
		// CommonBundle.INSTANCE.commonStyle().highlight());

		// objectShapes.get(key).setStrokeColor("red");

		if (objectHighlightAnimate != null) {
			objectHighlightAnimate.stop();
		}

		if (objectHighlightTimer != null) {
			objectHighlightTimer.cancel();
		}

		if (objectHighlight != null) {
			objectHighlight.setRadius(1);
		}

		objectHighlight = objectCircles.get(key);

		objectHighlightAnimate = new Animate(objectHighlight, "radius", 1, 3,
				HIGHLIGHT_INTERVAL);
		objectHighlightAnimate.start();

		objectHighlightTimer = new Timer() {

			@Override
			public void run() {
				objectHighlightAnimate.start();
			}
		};

		objectHighlightTimer.scheduleRepeating(HIGHLIGHT_INTERVAL);

		return;
	}

	public void highlightControlPoint(final Object key) {

		if (controlPointHighlightAnimate != null) {
			controlPointHighlightAnimate.stop();
		}

		if (controlPointHighlightTimer != null) {
			controlPointHighlightTimer.cancel();
		}

		if (controlPointHighlight != null) {
			controlPointHighlight.setRadius(1);
		}

		controlPointHighlight = controlPointCircles.get(key);

		controlPointHighlightAnimate = new Animate(controlPointHighlight,
				"radius", 1, 3, HIGHLIGHT_INTERVAL);
		controlPointHighlightAnimate.start();

		controlPointHighlightTimer = new Timer() {

			@Override
			public void run() {
				controlPointHighlightAnimate.start();
			}
		};

		controlPointHighlightTimer.scheduleRepeating(HIGHLIGHT_INTERVAL);

		return;
	}

	public FlexTable getObjectFlexTable() {
		return objectFlexTable;
	}

	public void addControlPoint(Object key, String create, String position,
			String layer, boolean enabled) {

		String[] coor = position.split("/");
		float x = Float.parseFloat(coor[0]);
		// float z = Float.parseFloat(coor[1]);
		float y = Float.parseFloat(coor[2]);

		mapDrawingArea.add(createControlPointCircle(key, x, y));

		return;
	}
}
